//
// Created by Administrator on 2023/5/28.
//
#pragma once
#ifndef CLOSE_BUTTON_H
#define CLOSE_BUTTON_H


#include <QPushButton>
#include <QMouseEvent>
#include "../../Qss/Close_Button_Q.h"


template <typename Owner>
class Close_Button: public QPushButton
{
public:
    Close_Button(Owner* owner);


    ~Close_Button();


    void Profile();


    void Redraw();


    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);


private:
    Close_Button();


    Owner* owner;


    bool close_press {false};

    int width {15};
    int height {15};
};


template <typename Owner>
Close_Button<Owner>::Close_Button(Owner* owner):
        owner(owner)
{
    this->Profile();
}


template <typename Owner>
Close_Button<Owner>::Close_Button(){}


template <typename Owner>
Close_Button<Owner>::~Close_Button(){}


template <typename Owner>
void Close_Button<Owner>::Profile()
{
    this->setParent(this->owner);


    this->setObjectName(QString("Close_Button"));


    this->setStyleSheet(c_b_qss);

    this->resize(this->width, this->height);
    this->move(this->owner->geometry().width() - 25, 7);
    this->setMouseTracking(true);

}


template <typename Owner>
void Close_Button<Owner>::Redraw()
{
    this->move(this->owner->geometry().width() - 25, 7);
}


template <typename Owner>
void Close_Button<Owner>::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        this->close_press =true;
    }
}


template <typename Owner>
void Close_Button<Owner>::mouseReleaseEvent(QMouseEvent* event)
{
    if(this->close_press)
    {
        this->owner->close();
    }
}




#endif //CLOSE_BUTTON_H
